// pages/vehicle/chachediaodu_nav/accept_orders/accept_orders.js
const mybehavior = require("../../../../behaviors/requests")
var time_task = null
var time_task1 = null
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    daiban_num: 0,
    list: [
    ],
    base_urlstr: "https://logisticsengineering.gree.com/package_integrated_api/reverse_manage",
    page_size: 2,
    page: 1,
    page_all: 0,
    total: 0,
    show: true,
    cancelShow: true



  },
  call(e){
    let row = e.currentTarget.dataset.row_data
    console.log(row);
    wx.makePhoneCall({
      phoneNumber: row, 
    })
  },
  // 获取超期未处理数据
  getExpireHandleData() {
    let self = this;
    let url = self.data.base_urlstr + "/query_orders2"
    let data = {
      page_size: 9999,
      page: 1,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        self.setData({
          daiban_num: res.data.total,
        })

      } else {
        self.setData({
          daiban_num: 0,
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "error"
        // })
      }
    }).catch(err => {
      self.setData({
        daiban_num: 0,
      })
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error'
      // })
    })
  },
  // 切换超期待处理
  expireHandle() {
    let self = this;
    clearInterval(time_task)
    clearInterval(time_task1)
    self.setData({
      show: false,
      cancelShow: false,
    })
    let url = self.data.base_urlstr + "/query_orders2"
    let data = {
      page_size: 9999,
      page: 1,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        var show_list = res.data.results
        self.setData({
          list: show_list,
          page_all: Math.ceil(res.data.total / 9999),
          total: res.data.total,
          daiban_num: res.data.total,
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
        self.setData({
          list: [],
        })
      }
    }).catch(err => {
      self.setData({
        list: [],
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 返回接单页面
  backAccept() {
    this.setData({
      show: true,
      cancelShow: true
    })
    this.getDataList()
    time_task = setInterval(this.getDataList, 3000)
    time_task1 = setInterval(this.getExpireHandleData, 3000)
  },
  // 分页
  onPageChange(e) {
    // console.log("xxx",e.detail.sonParam);
    let p = e.detail.sonParam
    this.setData({
      page: p,
    })
    this.getDataList()
  },
  // 获取数据
  getDataList() {
    let self = this;
    let url = self.data.base_urlstr + "/query_orders"
    let data = {
      page_size: self.data.page_size,
      page: self.data.page,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        var show_list = res.data.results
        self.setData({
          list: show_list,
          page_all: Math.ceil(res.data.total / self.data.page_size),
          total: res.data.total,
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
        self.setData({
          list: [],
        })
      }
    }).catch(err => {
      self.setData({
        list: [],
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 操作按钮
  operateBtn(e) {
    // console.log(e.currentTarget.dataset.row_data);
    let row = e.currentTarget.dataset.row_data
    if (row.status == "未接单") {
      this.receiveOrder(row)
    } else {
      this.arriveOrder(row)
    }
  },
  // 接单操作
  receiveOrder(e) {
    let self = this;
    let data = {
      key: e._id
    }
    console.log("1", data);
    let url = self.data.base_urlstr + "/take_order"
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        self.setData({
          show: true,
          cancelShow: true
        })
        wx.showToast({
          title: res.msg,
        })
        self.getDataList()
        self.getExpireHandleData()
      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 报完工操作
  arriveOrder(e) {
    let self = this;
    let data = {
      key: e._id
    }
    console.log(data);
    let url = self.data.base_urlstr + "/report_completion"
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
        })
        self.getDataList()

      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDataList()
    this.getExpireHandleData()
    time_task = setInterval(this.getDataList, 3000)
    time_task1 = setInterval(this.getExpireHandleData, 3000)


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    clearInterval(time_task)
    clearInterval(time_task1)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(time_task)
    clearInterval(time_task1)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})